#
# @lc app=leetcode.cn id=135 lang=python
#
# [135] 分发糖果
#

# @lc code=start
class Solution(object):
    def candy(self, ratings):
        """
        :type ratings: List[int]
        :rtype: int
        """
        candys = [1]*len(ratings)
        nums = 0 

        for i in range(1, len(ratings)):
            if ratings[i] > ratings[i-1]:
                candys[i] = candys[i-1]+1
        for i in range(len(ratings)-2, -1, -1):
            if ratings[i] > ratings[i+1]:
                candys[i] = max(candys[i], candys[i+1]+1)
            nums += candys[i]

        return nums+candys[-1]
        
# @lc code=end

